package com.hmall.service;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.*;
import com.hmall.model.*;
import com.hmall.vo.*;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigInteger;
import java.util.*;

/**
 * @Author zhoutao
 */
@Service
public class CommentService {
    
    private  MallCommentMapper mallCommentMapper;

    private  MallCommentDetailMapper mallCommentDetailMapper;

    private  MallCommentUserConnectMapper mallCommentUserConnectMapper;

    private  MallOrderMapper mallOrderMapper;

    private  MallOrderItemMapper mallOrderItemMapper;

    private  MallProductMapper mallProductMapper;

    private  SysUserMapper userMapper;

    private  MallHotMapper mallHotMapper;

    private  MallShopMapper mallShopMapper;

    @Autowired
    public CommentService(MallCommentMapper mallCommentMapper, MallCommentDetailMapper mallCommentDetailMapper, MallCommentUserConnectMapper mallCommentUserConnectMapper, MallOrderMapper mallOrderMapper, MallOrderItemMapper mallOrderItemMapper, MallProductMapper mallProductMapper, SysUserMapper userMapper, MallHotMapper mallHotMapper, MallShopMapper mallShopMapper) {
        this.mallCommentMapper = mallCommentMapper;
        this.mallCommentDetailMapper = mallCommentDetailMapper;
        this.mallCommentUserConnectMapper = mallCommentUserConnectMapper;
        this.mallOrderMapper = mallOrderMapper;
        this.mallOrderItemMapper = mallOrderItemMapper;
        this.mallProductMapper = mallProductMapper;
        this.userMapper = userMapper;
        this.mallHotMapper = mallHotMapper;
        this.mallShopMapper = mallShopMapper;
    }

    /**
     * @Date 2018/11/9 16:33
     * @Param [userId, productId, type, pageNum, pageSize]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.PageVo>
     * @Description // 列出某个产品的评价
     */
    public ServerResponse<PageVo<MallCommentVo>> listComment(Integer userId, Integer productId, String type, Integer pageNum, Integer pageSize) {

        List<MallComment> mallCommentList = mallCommentMapper.listComment(productId);

        List<MallCommentVo> mallCommentVoList = new ArrayList<>();
        for(int i = 0; i < mallCommentList.size(); i++){
            MallCommentVo mallCommentVo = new MallCommentVo();
            mallCommentVo.setCommentId(mallCommentList.get(i).getCommentId());

            Date currentTime = new Date();
            mallCommentVo.setCurrentTime(currentTime);
            mallCommentVo.setCommentTime(mallCommentList.get(i).getCommentTime());
            mallCommentVo.setEvaluate(mallCommentList.get(i).getEvaluate());
            mallCommentVo.setImages(mallCommentList.get(i).getImages());
            mallCommentVo.setStar(mallCommentList.get(i).getStar());
            mallCommentVo.setUserId(mallCommentList.get(i).getUserId());
            //昵称设置
            String nickName = userMapper.selectNickNameByUserId(mallCommentList.get(i).getUserId());
            int anoymous = mallCommentList.get(i).getAnoymous();
            String nowNickName = "";
            if(anoymous == 1){
                if(nickName.length() == 1){
                    nowNickName = "*";
                }else {
                    nowNickName = nickName.substring(0, 1);
                    nowNickName += "****";
                    nowNickName += nickName.substring(nickName.length() - 1, nickName.length());
                }
            }else{
                nowNickName = nickName;
            }

            mallCommentVo.setNickName(nowNickName);

            //标签设置
            String title = "";
            String name = mallProductMapper.selectValueByProductId(productId);
            String strValue[] = name.split(",", -1);
            title += strValue[0] + "-" + strValue[1] + "-" + strValue[2] + "-" + strValue[5] + "-" + strValue[6] + "-" + strValue[7] + "(" + strValue[3] + strValue[4] + ")";
            mallCommentVo.setTitle(title);

            //列出的评论必定是状态为交易完成的 50
            Date buyTime = mallOrderItemMapper.selectUpdateTime(mallCommentList.get(i).getOrderItemId());
            mallCommentVo.setBuyTime(buyTime);

            //回复数直接获得，不过评价之后要进行更新
            mallCommentVo.setReply(mallCommentList.get(i).getCommentNum());

           Double allRank = mallCommentList.get(i).getAllRank();
           mallCommentVo.setRank(allRank);
           mallCommentVo.setQuantityRank(mallCommentList.get(i).getQualityRank());
           mallCommentVo.setPackingRank(mallCommentList.get(i).getPackingRank());
           mallCommentVo.setSaleAfterRank(mallCommentList.get(i).getSaleAfterRank());
           mallCommentVo.setLogisticsRrank(mallCommentList.get(i).getLogisticsRank());

           //头像
           String avatar = userMapper.selectAvatarByUserId(mallCommentList.get(i).getUserId());
           mallCommentVo.setAvatar(avatar);
           //当前用户是否对该商品进行点赞
            MallCommentUserConnect mallCommentUserConnect = mallCommentUserConnectMapper.selectByCommentAndUser(mallCommentList.get(i).getCommentId(), userId);

            int isStar;
            if(mallCommentUserConnect == null){
                isStar = 0;
            }else {
                isStar = mallCommentUserConnect.getIsStar();
            }
           mallCommentVo.setHaveStar(isStar);


           mallCommentVoList.add(mallCommentVo);

        }

        //进行筛选
        List<MallCommentVo> afterFilterMallCommentVoList = new ArrayList<MallCommentVo>();
        for(MallCommentVo mallCommentVo : mallCommentVoList){
        if(type == null) {
            type = "";
        }
            switch (type){
                case "全部" :
                    afterFilterMallCommentVoList.add(mallCommentVo);
                    break;
                case "好评" :
                    if(mallCommentVo.getRank() >= 4){
                        afterFilterMallCommentVoList.add(mallCommentVo);
                    }
                    break;
                case "中评" :
                    if(mallCommentVo.getRank() >= 2 && mallCommentVo.getRank() < 4){
                        afterFilterMallCommentVoList.add(mallCommentVo);
                    }
                    break;
                case "差评" :
                    if(mallCommentVo.getRank() >= 0 && mallCommentVo.getRank() < 2){
                        afterFilterMallCommentVoList.add(mallCommentVo);
                    }
                    break;
                case "有图" :
                    if(mallCommentVo.getImages() != null){
                        afterFilterMallCommentVoList.add(mallCommentVo);
                    }
                    break;
                case "质量好" :
                    if(mallCommentVo.getQuantityRank() >= 4){
                        afterFilterMallCommentVoList.add(mallCommentVo);
                    }
                    break;
                case "包装优" :
                    if(mallCommentVo.getPackingRank() >= 4){
                        afterFilterMallCommentVoList.add(mallCommentVo);
                    }
                    break;
                case "售后好" :
                    if(mallCommentVo.getSaleAfterRank() >= 4){
                        afterFilterMallCommentVoList.add(mallCommentVo);
                    }
                    break;
                case "物流快" :
                    if(mallCommentVo.getLogisticsRrank() >= 4){
                        afterFilterMallCommentVoList.add(mallCommentVo);
                    }
                    break;
                case "有回复" :
                    if(mallCommentVo.getReply() > 0){
                        afterFilterMallCommentVoList.add(mallCommentVo);
                    }
                    break;
                default:afterFilterMallCommentVoList.add(mallCommentVo);

            }
        }

        int total = afterFilterMallCommentVoList.size();


        int start = pageSize * (pageNum - 1);
        int end = Math.min( start + pageSize, total);
        List<MallCommentVo> subAfterFilterMallCommentVoList = afterFilterMallCommentVoList.subList(start, end);
        PageVo<MallCommentVo> pageVo = new PageVo<>(pageNum, pageSize, total, subAfterFilterMallCommentVoList);
        return ServerResponse.createBySuccess(pageVo);
    }

    /**
     * @Date 2018/11/9 16:34
     * @Param [mallCommentOrderVo]
     * @return com.hmall.common.ServerResponse
     * @Description //
     * 用户更新评论
     * 更新的是订单的评价
     */
    public ServerResponse updateComment(MallCommentOrderVo mallCommentOrderVo) {

        if(mallCommentOrderVo.getHaveComment() == 1){
            return ServerResponse.createByErrorMessage("该订单已经评价过");
        }

        int packingRank = mallCommentOrderVo.getPackingRank();
        int logisticsRank = mallCommentOrderVo.getLogisticsRank();
        int serviceRank = mallCommentOrderVo.getServiceRank();
        int qualityRank = mallCommentOrderVo.getQualityRank();
        int saleAfterRank = mallCommentOrderVo.getSaleAfterRank();
        int technicalAfterRank = mallCommentOrderVo.getTechnicalAfterRank();
        double rank = 1.0 * (packingRank + logisticsRank + serviceRank + qualityRank + saleAfterRank + technicalAfterRank) / 6;

        JSONArray jsonArray = JSONArray.fromObject(mallCommentOrderVo.getMallCommentUpdateJsonStr());
        List<MallCommentUpdateVo> mallCommentUpdateVoList = JSONArray.toList(jsonArray, MallCommentUpdateVo.class);


        //不会批量更新
        for(MallCommentUpdateVo mallCommentUpdateVo : mallCommentUpdateVoList){

            mallCommentMapper.updateComment(mallCommentUpdateVo.getCommentId(),mallCommentUpdateVo.getAnoymous(), mallCommentUpdateVo.getEvaluate(), mallCommentUpdateVo.getImages(), packingRank, logisticsRank,
                    serviceRank, qualityRank, saleAfterRank, technicalAfterRank, rank);
        }

        //更新评价之后 要改为已经评论过了无法再次评论，更改状态
        for(MallCommentUpdateVo mallCommentUpdateVo : mallCommentUpdateVoList){

             mallOrderItemMapper.updateCommentCheck(mallCommentUpdateVo.getCommentId());
        }


        return ServerResponse.createBySuccess();
    }

    public ServerResponse<MallCommentTitle> listFilter(Integer productId) {

        //0.全部
        //1.好评
        //2.中评
        //3.差评
        //4.有图
        //5.质量好
        //6.包装优
        //7.售后好
        //8.物流快
        //9.有回复
        List<MallCommentFilter> mallCommentFilterList = new ArrayList<>();

        //某个产品的评价列表
        List<MallComment> mallCommentList = mallCommentMapper.selectByProductId(productId);
        int allComment = mallCommentMapper.selectCountByProduct(productId);

        int good = 0, mid = 0, bad = 0, haveImg = 0, goodQuality = 0, goodPacking = 0, goodSaleAfter = 0, goodLogistics = 0, commentNum = 0;

        for(MallComment mallComment : mallCommentList){
            if(mallComment.getAllRank() >= 4){
                good++;
            }else if(mallComment.getAllRank() >= 2){
                mid++;
            }else{
                bad++;
            }

            if(mallComment.getImages() != null){
                haveImg++;
            }

            if(mallComment.getQualityRank() >= 4){
                goodQuality++;
            }

            if(mallComment.getPackingRank() >= 4){
                goodPacking++;
            }

            if(mallComment.getSaleAfterRank() >= 4){
                goodSaleAfter++;
            }

            if(mallComment.getLogisticsRank() >= 4){
                goodLogistics++;
            }

            if(mallComment.getCommentNum() > 0){
                commentNum++;
            }

        }
        mallCommentFilterList.add(new MallCommentFilter("全部", allComment));
        mallCommentFilterList.add(new MallCommentFilter("好评", good));
        mallCommentFilterList.add(new MallCommentFilter("中评", mid));
        mallCommentFilterList.add(new MallCommentFilter("差评", bad));
        mallCommentFilterList.add(new MallCommentFilter("有图", haveImg));
        mallCommentFilterList.add(new MallCommentFilter("质量好", goodQuality));
        mallCommentFilterList.add(new MallCommentFilter("包装优", goodPacking));
        mallCommentFilterList.add(new MallCommentFilter("售后好", goodSaleAfter));
        mallCommentFilterList.add(new MallCommentFilter("物流快", goodLogistics));
        mallCommentFilterList.add(new MallCommentFilter("有回复", commentNum));

        //平均分计算

       /* 计算平均分时，当用户评分为0分条数超过总条数8%时，一律只按8%取计算条数，且根据以上自动调整用户评分总条数计算参数；
        计算平均分时，当用户评分为1分条数超过总条数10%时，一律只按10%取计算条数，且根据以上自动调整用户评分总条数计算参数；*/

       MallHot mallHot = mallHotMapper.selectByProductId(productId);
       Double rank = 5.0;
       if(mallHot != null){
           if(mallHot.getCommentRank() != null){
               rank = mallHot.getCommentRank();
           }
       }


        MallCommentTitle mallCommentTitle = new MallCommentTitle(rank, mallCommentFilterList);
        return ServerResponse.createBySuccess(mallCommentTitle);
    }

    public ServerResponse<MallCommentDetailVo> listBasicCommentDetail(Integer userId, Integer commentId) {

        //————————————————
        //基础评论信息

        MallComment mallComment = mallCommentMapper.selectByPrimaryKey(commentId);

        MallCommentDetailVo mallCommentDetailVo = new MallCommentDetailVo();

        Date date = new Date();
        mallCommentDetailVo.setCurrentTime(date);
        mallCommentDetailVo.setCommentTime(mallComment.getCommentTime());
        mallCommentDetailVo.setEvaluate(mallComment.getEvaluate());
        mallCommentDetailVo.setImages(mallComment.getImages());
        mallCommentDetailVo.setUserId(mallComment.getUserId());
        //头像设置
        mallCommentDetailVo.setAvatar(userMapper.selectAvatarByUserId(mallComment.getUserId()));
        //昵称设置
        String nickName = userMapper.selectNickNameByUserId(mallComment.getUserId());
        int anonymous = mallComment.getAnoymous();
        String nowNickName;
        if(anonymous == 1){
            if(nickName.length() == 1){
                nowNickName = "*";
            }else {
                nowNickName = nickName.substring(0, 1);
                nowNickName += "****";
                nowNickName += nickName.substring(nickName.length() - 1, nickName.length());
            }
        }else{
            nowNickName = nickName;
        }
        mallCommentDetailVo.setNickName(nowNickName);

        //点赞数目
        mallCommentDetailVo.setStar(mallComment.getStar());
        //评分设置
        mallCommentDetailVo.setRank(mallComment.getAllRank());

        //标签设置
        String title = "";
        int productId = mallOrderItemMapper.selectProductByOrderItem(mallComment.getOrderItemId());
        String name = mallProductMapper.selectValueByProductId(productId);
        String []strValue = name.split(",", -1);
        title += strValue[0] + "-" + strValue[1] + "-" + strValue[2] + "-" + strValue[5] + "-" + strValue[6] + "-" + strValue[7] + "(" + strValue[3] + strValue[4] + ")";
        mallCommentDetailVo.setTitle(title);

        //回复数
        mallCommentDetailVo.setReply(mallComment.getCommentNum());

        //该用户是否进行点赞

        //当前用户是否对该商品进行点赞
        MallCommentUserConnect mallCommentUserConnect = mallCommentUserConnectMapper.selectByCommentAndUser(mallComment.getCommentId(), userId);

        int isStar;
        if(mallCommentUserConnect == null){
            isStar = 0;
        }else {
            isStar = mallCommentUserConnect.getIsStar();
        }
        mallCommentDetailVo.setHaveStar(isStar);


        //——————————————————————————————————
        //产品信息

        MallProduct mallProduct = mallProductMapper.selectByProductId(productId);

        MallCommentProductVo mallCommentProductVo = new MallCommentProductVo();

        mallCommentProductVo.setImg(mallProduct.getMainImage());
        mallCommentProductVo.setProductId(productId);

        String[] str = mallProduct.getName().split(",", -1);
        title += str[0] + str[3] + str[4] + str[1];
        mallCommentProductVo.setTitle(title);

        mallCommentProductVo.setPrice(mallProduct.getPrice().doubleValue());
        //产品评分
        Double commentRank = mallHotMapper.selectByProductId(productId).getCommentRank();
        mallCommentProductVo.setCommentRank(commentRank);
        mallCommentDetailVo.setMallCommentProductVoList(mallCommentProductVo);


        return ServerResponse.createBySuccess(mallCommentDetailVo);
    }

    /**
     * @Date 2018/11/9 16:36
     * @Param [userId, commentId, evaluate, anoymous, replyName, replyId]
     * @return com.hmall.common.ServerResponse
     * @Description //
     * 评论的回复
     *     插入回复数据
     */
    public ServerResponse replyComment(Integer userId, Integer commentId, String evaluate, Integer anoymous, String replyName, Integer replyId) {

            MallCommentDetail mallCommentDetail = new MallCommentDetail();
            mallCommentDetail.setCommentId(commentId);
            mallCommentDetail.setReplyName(replyName);
            mallCommentDetail.setUserId(userId);
            mallCommentDetail.setAnoymous(anoymous);
            mallCommentDetail.setEvaluate(evaluate);
            mallCommentDetail.setReplyId(replyId);
            mallCommentDetailMapper.myInsert(mallCommentDetail);

            //回复之后刷新mall_comment表中的comment_num

          //回复一次，加1
         Date commentTime = mallCommentMapper.selectByPrimaryKey(commentId).getCommentTime();
           mallCommentMapper.updateCommentNum(commentId, commentTime);

           return ServerResponse.createBySuccess();
    }

    /**
     * @Date 2018/11/9 16:36
     * @Param [order_id]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.MallCommentOrderVo>
     * @Description //
     * 得到评价信息
     */
    public ServerResponse<MallCommentOrderVo> getComment(Integer orderId) {

        //先查询出该order_id的order_no,然后根据order_no去订单详情表中查询所有的订单详情
        List<BigInteger> orderNoList = mallOrderMapper.listOrderNoList(orderId);

        Integer shopId = mallOrderMapper.selectShopIdByOrderId(orderId);
        String shopName = mallShopMapper.selectShopNameByOrderId(shopId);
        //批量查询出订单详情
        List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectByOrderNoList(orderNoList);
        //如果已经进行过评论了

        //都是同步的

            //如果还未评论，返回默认值,为评论的更新提供默认数据

            //如果已经评论，返回评论的内容
            List<Integer> mallOrderItemIdList = new ArrayList<>();
            for(MallOrderItem mallOrderItem : mallOrderItemList){
                mallOrderItemIdList.add(mallOrderItem.getId());
            }

            //评价列表
            List<MallComment> mallCommentList = mallCommentMapper.selectByOrderItemIdList(mallOrderItemIdList);

            List<MallCommentUpdateVo> mallCommentUpdateVoList = new ArrayList<>();
            for(MallComment mallComment : mallCommentList){
                //标签设置
                String title = "";
                String name = mallProductMapper.selectValueByOrderItemId(mallComment.getOrderItemId());
                String []strValue = name.split(",", -1);
                int productId = mallOrderItemMapper.selectProductByOrderItem(mallComment.getOrderItemId());
                MallProduct mallProduct = mallProductMapper.selectByProductId(productId);
                title += strValue[0] + "-" + strValue[1] + "-" + strValue[2] + "-" + strValue[5] + "-" + strValue[6] + "-" + strValue[7] + "(" + strValue[3] + strValue[4] + ")";
                mallCommentUpdateVoList.add(new MallCommentUpdateVo(mallComment.getCommentId(), mallProduct.getMainImage(), title, mallComment.getEvaluate(), mallComment.getImages(), mallComment.getAnoymous()));
            }

            //重新封装
            MallCommentOrderVo mallCommentOrderVo = new MallCommentOrderVo();
            mallCommentOrderVo.setLogisticsRank(mallCommentList.get(0).getLogisticsRank());
            mallCommentOrderVo.setPackingRank(mallCommentList.get(0).getPackingRank());
            mallCommentOrderVo.setQualityRank(mallCommentList.get(0).getQualityRank());
            mallCommentOrderVo.setSaleAfterRank(mallCommentList.get(0).getSaleAfterRank());
            mallCommentOrderVo.setServiceRank(mallCommentList.get(0).getServiceRank());
            mallCommentOrderVo.setTechnicalAfterRank(mallCommentList.get(0).getTechnicalAfterRank());
            mallCommentOrderVo.setShopName(shopName);
            //是否评论过
            int check;
            if(mallOrderItemList.get(0).getCommentCheck() == null) {
                check = 0;
            }
            else{
                check = mallOrderItemList.get(0).getCommentCheck();
            }
            mallCommentOrderVo.setHaveComment(check);

            JSONArray json = JSONArray.fromObject(mallCommentUpdateVoList);
            String str = json.toString();
            mallCommentOrderVo.setMallCommentUpdateJsonStr(str);

            System.out.println("mallComment" + mallCommentOrderVo);
            return ServerResponse.createBySuccess(mallCommentOrderVo);
        }


    public ServerResponse<PageVo<MallCommentReplyDetail>> listAdvancedCommentDetail(Integer commentId, Integer pageNum, Integer pageSize) {


        //——————————————————————————
        //评论子回复

        List<MallCommentDetail> mallCommentDetailList;

        //列出某条评论的回复详情
        // 要根据时间进行排序
        mallCommentDetailList = mallCommentDetailMapper.selectByCommentId(commentId);
        List<MallCommentReplyDetail> mallCommentReplyDetailList = new ArrayList<>();
        for(MallCommentDetail mallCommentDetail : mallCommentDetailList){
            Integer userId = mallCommentDetail.getUserId();
            //回复人的头像
            String avatar = userMapper.selectAvatarByUserId(mallCommentDetail.getUserId());

            //回复人的昵称
            String subNickName = userMapper.selectNickNameByUserId(mallCommentDetail.getUserId());
            //判断用户是否匿名
            String nowSubNickName = "";

            if(1 == mallCommentDetail.getAnoymous()){
                if(subNickName.length() == 1){
                    nowSubNickName = "*";
                }else {
                    nowSubNickName = subNickName.substring(0, 1);
                    nowSubNickName += "****";
                    nowSubNickName += subNickName.substring(subNickName.length() - 1, subNickName.length());
                }
            }else{
                nowSubNickName = subNickName;
            }

            mallCommentReplyDetailList.add(new MallCommentReplyDetail(userId, avatar, nowSubNickName, mallCommentDetail.getEvaluate(), new Date(), mallCommentDetail.getCommentTime(), mallCommentDetail.getReplyName(),mallCommentDetail.getReplyId()));
        }

        int total = mallCommentDetailList.size();

        //数据数目
        int size = mallCommentDetailList.size();

        System.out.println(size);
        int start = pageSize * (pageNum - 1);
        int end = Math.min(start + pageSize, size);
        List<MallCommentReplyDetail> subMallCommentReplyDetailList = mallCommentReplyDetailList.subList(start, end);
        PageVo<MallCommentReplyDetail> pageVo = new PageVo<>(pageNum, pageSize, total, subMallCommentReplyDetailList);
        return ServerResponse.createBySuccess(pageVo);
    }

    /**
     * @Date 2018/11/9 16:37
     * @Param [orderNo]
     * @return com.hmall.common.ServerResponse
     * @Description //
     * 插入评论
     */
    public ServerResponse createDefaultComment(Long orderNo) {

        //如果查询不到订单号，抛异常
        int count = null == mallOrderMapper.selectByOrderNo(orderNo) ? -1 : 1;
        if(count <= 0){
            return ServerResponse.createByErrorMessage("找不到该订单号，请重新输入");
        }

        List<MallOrderItem> orderItemList = mallOrderItemMapper.selectAllByOrderNo(orderNo);

        //评论设置默认值
        MallComment mallComment = new MallComment();
        for(MallOrderItem mallOrderItem : orderItemList){
            int orderItemId = mallOrderItem.getId();

            mallComment.setAnoymous(1);
            mallComment.setStar(0);
            mallComment.setUserId(mallOrderItem.getUserId());
            mallComment.setCommentNum(0);
            mallComment.setLogisticsRank(5);
            mallComment.setPackingRank(5);
            mallComment.setQualityRank(5);
            mallComment.setServiceRank(5);
            mallComment.setSaleAfterRank(5);
            mallComment.setTechnicalAfterRank(5);
            mallComment.setAllRank(5.0);
            mallComment.setOrderItemId(orderItemId);
            System.out.println(mallComment);

            MallComment mallComment1 = mallCommentMapper.selectByOrderItemId(mallOrderItem.getId());
            if(mallComment1 == null) {
                mallCommentMapper.myInsert(mallComment);
            }
        }

        return ServerResponse.createBySuccess();
    }

    public ServerResponse doStar(Integer userId, Integer commentId) {

        MallCommentUserConnect mallCommentUserConnect = mallCommentUserConnectMapper.selectByUserIdCommentId(userId, commentId);
        if(mallCommentUserConnect != null) {
            return ServerResponse.createByErrorMessage("您已经进行过点赞");
        }
        //不能进行更新，所有都是插入操作
        mallCommentUserConnectMapper.doStar(userId, commentId);

        mallCommentMapper.updateStar(commentId);
       return ServerResponse.createBySuccess();
    }

    public ServerResponse updateAveGrade() {

        List<Integer> productIdList;
        productIdList = mallProductMapper.listAllProductId();

        for(Integer productId : productIdList) {

            List<MallComment> mallCommentList = mallCommentMapper.selectByProductId(productId);
            if(mallCommentList.size() == 0) {
                continue;
            }
            //总评论数目
            int rankSum = 0;

            int maxOneNum = (int) (rankSum * 0.08);
            int maxTwoNum = (int) (rankSum * 0.1);

            //所有分数加在一起
            int allGrade = 0;

            //包装评分
            int packingOneNum = 0;
            int packingTwoNum = 0;
            //物流评分
            int logisticsOneNum = 0;
            int logisticsTwoNum = 0;
            //卖家服务等级评分
            int serviceOneNum = 0;
            int serviceTwoNum = 0;
            //质量等级评分
            int qualityOneNum = 0;
            int qualityTwoNum = 0;
            //售后等级评分
            int saleAfterOneNum = 0;
            int saleAfterTwoNum = 0;
            //技术售后评分
            int techAfterOneNum = 0;
            int techAfterTwoNum = 0;

            for (MallComment mallComment : mallCommentList) {

                int pRank = mallComment.getPackingRank();
                int lRank = mallComment.getLogisticsRank();
                int sRank = mallComment.getServiceRank();
                int qRank = mallComment.getQualityRank();
                int aRank = mallComment.getSaleAfterRank();
                int tRank = mallComment.getTechnicalAfterRank();

                //-------------------------------------------------
                //包装
                if (pRank == 1 && (1 + packingOneNum) <= maxOneNum) {
                    packingOneNum++;
                    allGrade += pRank;
                    rankSum++;
                }
                if (pRank == 2 && (1 + packingTwoNum) <= maxTwoNum) {
                    packingTwoNum++;
                    allGrade += pRank;
                    rankSum++;
                }
                if (pRank >= 3) {
                    allGrade += pRank;
                    rankSum++;
                }
                //--------------------------------------------------
                //物流
                if (lRank == 1 && (1 + logisticsOneNum) <= maxOneNum) {
                    logisticsOneNum++;
                    allGrade += lRank;
                    rankSum++;
                }
                if (lRank == 2 && (1 + saleAfterTwoNum) <= maxTwoNum) {
                    saleAfterTwoNum++;
                    allGrade += lRank;
                    rankSum++;
                }
                if (lRank >= 3) {
                    allGrade += lRank;
                    rankSum++;
                }
                //-------------------------------------------------
                //服务
                if (qRank == 1 && (1 + serviceOneNum) <= maxOneNum) {
                    serviceOneNum++;
                    allGrade += qRank;
                    rankSum++;
                }
                if (qRank == 2 && (1 + serviceTwoNum) <= maxTwoNum) {
                    serviceTwoNum++;
                    allGrade += qRank;
                    rankSum++;
                }
                if (qRank >= 3) {
                    allGrade += qRank;
                    rankSum++;
                }
                //-------------------------------------------------
                //质量
                if (qRank == 1 && (1 + qualityOneNum) <= maxOneNum) {
                    qualityOneNum++;
                    allGrade += qRank;
                    rankSum++;
                }
                if (qRank == 2 && (1 + qualityTwoNum) <= maxTwoNum) {
                    qualityTwoNum++;
                    allGrade += qRank;
                    rankSum++;
                }
                if (qRank >= 3) {
                    allGrade += qRank;
                    rankSum++;
                }
                //-------------------------------------------------
                //售后
                if (aRank == 1 && (1 + saleAfterOneNum) <= maxOneNum) {
                    saleAfterOneNum++;
                    allGrade += aRank;
                    rankSum++;
                }
                if (aRank == 2 && (1 + saleAfterTwoNum) <= maxTwoNum) {
                    saleAfterTwoNum++;
                    allGrade += aRank;
                    rankSum++;
                }
                if (aRank >= 3) {
                    allGrade += aRank;
                    rankSum++;
                }
                //-------------------------------------------------
                //技术
                if (tRank == 1 && (1 + techAfterOneNum) <= maxOneNum) {
                    techAfterOneNum++;
                    allGrade += tRank;
                    rankSum++;
                }
                if (tRank == 2 && (1 + techAfterTwoNum) <= maxTwoNum) {
                    techAfterTwoNum++;
                    allGrade += tRank;
                    rankSum++;
                }
                if (tRank >= 3) {
                    allGrade += tRank;
                    rankSum++;
                }
                //-------------------------------------------------


                //平均分即 allGrade/(5*评论数*6)
//
            }
          Double ave = allGrade/(rankSum) * 1.0;

             mallHotMapper.updateCommentRank(productId, ave);
        }

        return ServerResponse.createBySuccess();
    }

    /**
     * @Date 2018/11/9 16:38
     * @Param [userId]
     * @return com.hmall.common.ServerResponse<java.util.List<com.hmall.vo.MallCommentNeedVo>>
     * @Description //
     * 待评价列表
     */
    public ServerResponse<List<MallCommentNeedVo>> needComment(Integer userId) {

        List<MallCommentNeedVo> mallCommentNeedVoList = new ArrayList<>();

        List<MallOrder> mallOrderList = mallOrderMapper.selectNoComment(userId);
        for(MallOrder mallOrder : mallOrderList){
            MallCommentNeedVo mallCommentNeedVo = new MallCommentNeedVo();
            mallCommentNeedVo.setOrderNo(mallOrder.getOrderNo());
            mallCommentNeedVo.setOrderId(mallOrder.getId());
            List<Map<String, String>> mapList = new ArrayList<>();
            String shopName = mallShopMapper.selectShopNameByOrderId(mallOrder.getShopId());
            mallCommentNeedVo.setShopName(shopName);
            List<MallOrderItem> mallOrderItemList;
            mallOrderItemList = mallOrderItemMapper.selectOrderItemByOrderNo(mallOrder.getOrderNo());
            for(MallOrderItem mallOrderItem : mallOrderItemList){
                Map<String ,String> map = new HashMap<>();
                map.put("name", mallOrderItem.getProductName());
                map.put("img", mallOrderItem.getProductImage());
                mapList.add(map);
            }
            mallCommentNeedVo.setProductList(mapList);
            mallCommentNeedVoList.add(mallCommentNeedVo);
        }


        return ServerResponse.createBySuccess(mallCommentNeedVoList);
    }
}
